package com.jspgou.cms.action.store;

import static com.jspgou.cms.Constants.COMMON_SYS;
import static com.jspgou.cms.Constants.STORE_SYS;

import java.sql.BatchUpdateException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.jspgou.cms.entity.Adspace;
import com.jspgou.cms.entity.Brand;
import com.jspgou.cms.entity.ShopMember;
import com.jspgou.cms.entity.Store;
import com.jspgou.cms.manager.AdspaceMng;
import com.jspgou.cms.manager.ProductMng;
import com.jspgou.cms.web.CmsUtils;
import com.jspgou.cms.web.ShopFrontHelper;
import com.jspgou.cms.web.SiteUtils;
import com.jspgou.common.web.springmvc.MessageResolver;
import com.jspgou.core.entity.Website;
import com.jspgou.core.web.WebErrors;
import com.jspgou.core.web.front.FrontHelper;

@Controller
public class AdspaceFroAct {
    /**
     * 店铺广告版位列表页
    */
    public static final String TPL_ADVERTISE_SPACE_LIST = "tpl.advertiseSpaceList";
   /**
     * 店铺广告版位添加页
     */
    public static final String TPL_ADVERTISE_SPACE_ADD = "tpl.advertiseSpaceAdd";
    /**
     * 店铺广告版位修改页
     */
    public static final String TPL_ADVERTISE_SPACE_EDIT = "tpl.advertiseSpaceEdit";
	
	
    //广告版位列表
	@RequestMapping(value = "/adspace/list*.jspx")
	public String list(String cl,Integer pageNo, HttpServletRequest request, ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member=CmsUtils.getMember(request);
		if(member==null){
			return "redirect:../login.jspx";
		}
		Store store=member.getStore();
		model.addAttribute("cl", cl);
		model.addAttribute("store", store);
		ShopFrontHelper.setCommonData(request, model, web);
		ShopFrontHelper.frontPageData(request, model);
		return web.getTplSys(STORE_SYS, MessageResolver.getMessage(request,TPL_ADVERTISE_SPACE_LIST), request);
	}
	
    //广告版位添加
	@RequestMapping(value = "/adspace/add.jspx")
	public String add(HttpServletRequest request, ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member=CmsUtils.getMember(request);
		if(member==null){
			return "redirect:../login.jspx";
		}
		Store store=member.getStore();
		model.addAttribute("store", store);
		ShopFrontHelper.setCommonData(request, model, web);
		ShopFrontHelper.frontPageData(request, model);
		return web.getTplSys(STORE_SYS, MessageResolver.getMessage(request,TPL_ADVERTISE_SPACE_ADD), request);
	}
	
    //广告版位保存
	@RequestMapping(value = "/adspace/save.jspx" ,method = RequestMethod.POST)
	public String save(Adspace bean,HttpServletRequest request, ModelMap model) {
		ShopMember member=CmsUtils.getMember(request);
		if(member==null){
			return "redirect:../login.jspx";
		}
		Store store=member.getStore();
		bean.setStore(store);
		manager.save(bean);
		return add(request,model);
	}
	
    //广告版位修改
	@RequestMapping(value = "/adspace/edit.jspx")
	public String edit(Long id,HttpServletRequest request, ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member=CmsUtils.getMember(request);
		if(member==null){
			return "redirect:../login.jspx";
		}
		Store store=member.getStore();
		model.addAttribute("store", store);
		Adspace bean=manager.findById(id);
		model.addAttribute("bean", bean);
		ShopFrontHelper.setCommonData(request, model, web);
		ShopFrontHelper.frontPageData(request, model);
		return web.getTplSys(STORE_SYS, MessageResolver.getMessage(request,TPL_ADVERTISE_SPACE_EDIT), request);
	}
	
    //广告版位更新
	@RequestMapping(value = "/adspace/update.jspx",method = RequestMethod.POST)
	public String update(Adspace bean,Integer pageNo,HttpServletRequest request, ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member=CmsUtils.getMember(request);
		if(member==null){
			return "redirect:../login.jspx";
		}
		manager.update(bean);
		ShopFrontHelper.setCommonData(request, model, web);
		ShopFrontHelper.frontPageData(request, model);
		return "redirect:adspace/list.jspx";
	}
	
	//广告版位删除
	@RequestMapping("/adspace/delete.jspx")
	public String qqDelete(Long[] ids, Integer pageNo, HttpServletRequest request,
			ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		WebErrors errors = WebErrors.create(request);
		ShopFrontHelper.setCommonData(request, model, web);
		Adspace[] bean;
		try{			
			bean=manager.deleteByIds(ids);
		}catch(Exception e){
		    errors.addError(productMng.getTipFile("Please.delete.the.advertisement.to.contain.advertising.management.data"));
		    return FrontHelper.showError(errors, web, model, request);
		}
		return "redirect:adspace/list.jspx";
	}
	
	@Autowired
	private ProductMng productMng;
	@Autowired
	private AdspaceMng manager; 

}
